package com.dgut.epidemic.service;

import com.dgut.epidemic.mapper.WeChatUserMapper;
import com.dgut.epidemic.pojo.Daily_data;
import com.dgut.epidemic.pojo.WeChatUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Author: wuhaoduan
 * Date: 2021/6/6 10:20
 */
@Service
public class WeChatUserServiceImpl implements WeChatUserService{

    @Autowired
    private WeChatUserMapper weChatUserMapper;

    //判断账号密码是否正确
    @Override
    public int judgeWeChatUser(String id, String password){
        return weChatUserMapper.judgeWeChatUser(id,password);
    }

    //根据id获得用户数据
    @Override
    public WeChatUser getWeChatUserById(String id) {
        return weChatUserMapper.getWeChatUserById(id);
    }

    //判断某人当天是否已经上报
    @Override
    public int judgeUserIsSubmit(String id) {
        return weChatUserMapper.judgeUserIsSubmit(id);
    }

    //提交每日疫情填报
    @Override
    @Transactional
    public void insertDailyData(Daily_data daily_data){
        if(weChatUserMapper.judgeUserIsSubmit(daily_data.getSno()) == 0) {
            if(weChatUserMapper.judgeUserIsSubmitYes(daily_data.getSno())==0){
                weChatUserMapper.updateUserCountChange(1,daily_data.getSno());
            }
            else{
                weChatUserMapper.updateUserCount(daily_data.getSno());
            }
            weChatUserMapper.insertDailyData(daily_data);
        }
    }

    @Override
    @PostConstruct
    @Scheduled(cron = "0 0 1 * * ?")
    //每日凌晨对前一天未打卡的人打卡数置为0
    public void setAllNoWeChatUser() {
        List<WeChatUser> weChatUsers = weChatUserMapper.getAllWeChatUser();
        for(int i = 0;i < weChatUsers.size(); i++){
            if(weChatUserMapper.judgeUserIsSubmitYes(weChatUsers.get(i).getId())==0){
                weChatUserMapper.updateUserCountChange(0,weChatUsers.get(i).getId());
            }
        }
    }

}
